package net.arvin.mobilebanksystem.fragment;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.arvin.dao.DialogListAdapter;
import net.arvin.dao.ResponseHandler;
import net.arvin.mobilebanksystem.R;
import net.arvin.model.Recharge;
import net.arvin.service.RechargeService;
import net.arvin.utils.NetConnectionUtil;
import net.arvin.utils.PreferenceUtils;
import net.arvin.view.ListDialog;
import net.arvin.view.ListDialog.MyDialogListListener;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;

public class RechargeFragment extends Fragment implements OnClickListener {
	private RelativeLayout rechargeLayout;
	private TextView rechargeMoney;
	private TextView rechargePrice;
	private EditText rechargeNum;
	private Button rechargeBtn;
	// 默认是选中50元
	private int selectPosition = 3;

	private ListDialog mListDialog;
	private List<Map<String, String>> data;
	private final String[] money = { "10", "20", "30", "50", "100", "200",
			"300", "500" };
	private final String[] price = { "10.00", "20.00", "30.00", "49.95",
			"99.90", "199.60", "299.50", "499.00" };

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_recharge, null);
		init(view);
		return view;
	}

	private void init(View view) {
		initView(view);
		initListener();
		updateData();
	}

	private void updateData() {
		data = new ArrayList<Map<String, String>>();
		for (int i = 0; i < money.length; i++) {
			Map<String, String> map = new HashMap<String, String>();
			map.put(DialogListAdapter.MONEY_KEY, money[i]);
			map.put(DialogListAdapter.PRICE_KEY, price[i]);
			if (selectPosition == i) {
				map.put(DialogListAdapter.ISSELECT, "1");
			} else {
				map.put(DialogListAdapter.ISSELECT, "0");
			}
			data.add(map);
		}
	}

	private void initListener() {
		rechargeLayout.setOnClickListener(this);
		rechargeBtn.setOnClickListener(this);
	}

	private void initView(View view) {
		rechargeLayout = (RelativeLayout) view
				.findViewById(R.id.recharge_money_layout);
		rechargeMoney = (TextView) view.findViewById(R.id.rechargeMoney);
		rechargePrice = (TextView) view.findViewById(R.id.recharge_price);

		rechargeNum = (EditText) view.findViewById(R.id.rechargeNum);
		rechargeBtn = (Button) view.findViewById(R.id.rechargeBtn);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.recharge_money_layout:
			showListDialog();
			break;
		case R.id.rechargeBtn:
			dataChecked();
			break;
		}
	}

	private void dataChecked() {
		String phonenumber = rechargeNum.getText().toString().trim();
		if (phonenumber == null || phonenumber.trim().equals("")) {
			Toast.makeText(getActivity(), "手机号不能为空~", Toast.LENGTH_SHORT)
					.show();
			return;
		}
		if (phonenumber.trim().length() != 11) {
			Toast.makeText(getActivity(), "手机号长度不对~", Toast.LENGTH_SHORT)
					.show();
			return;
		}
		if (NetConnectionUtil.isConnectWithTip(getActivity(), "没有联网=_=!")) {
			RechargeService.tryRechargeMoney(getRechargeJSON(phonenumber),
					getResponseHandler(), getActivity());
		}
	}

	private ResponseHandler getResponseHandler() {
		{
			ResponseHandler mResponseHandler = new ResponseHandler() {
				@Override
				public void onRequsetSucceeded(final Object data) {
					Toast.makeText(getActivity(), "充值成功~",
							Toast.LENGTH_SHORT).show();
				}

				@Override
				public void onRequestFailed(final Object data) {
					Toast.makeText(getActivity(), "该手机号已被使用!",
							Toast.LENGTH_SHORT).show();
				}
			};
			return mResponseHandler;
		}
	}

	private String getRechargeJSON(String phonenumber) {
		Recharge recharge = new Recharge();
		recharge.setRechargeNum(phonenumber);
		recharge.setRechargeMoney(rechargeMoney.getText().toString());
		recharge.setRechargePrice(rechargePrice.getText().toString());
		recharge.setWhoRecharge(PreferenceUtils
				.getPreferPhonenumber(getActivity()));
		return JSON.toJSONString(recharge);
	}

	private void showListDialog() {
		mListDialog = new ListDialog(getActivity(), R.style.MyDialog,
				new MyDialogListListener() {

					@Override
					public void onClick(int position) {
						rechargeMoney.setText(data.get(position).get(
								DialogListAdapter.MONEY_KEY));
						rechargePrice.setText(data.get(position).get(
								DialogListAdapter.PRICE_KEY));
						selectPosition = position;
						updateData();
						mListDialog.dismiss();
					}
				});
		mListDialog.list = (ListView) mListDialog.getWindow().findViewById(
				R.id.dialog_list);
		mListDialog.list.setAdapter(new DialogListAdapter(getActivity(), data));
		mListDialog.setOnItemClickListener();
		mListDialog.show();
	}
}
